package com.wryyyyy.scoket;

import cn.hutool.json.JSONUtil;
import com.wryyyyy.dto.RealDTO;
import com.wryyyyy.dto.SessionBean;
import com.wryyyyy.service.ICountryService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.TextMessage;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class Task {
    @Resource
    private ICountryService countryService;

    /**
     * 整个泰安市 污水、净水、循环水 达标率
     */
    @Async
    @Scheduled(fixedDelay = 3600000) //1000=1秒
//    @Scheduled(fixedDelay = 10000) //1000=1秒
    public void passing() throws IOException {
        // 获取现在的用户session集合
        Map<String, SessionBean> sessionBeanMap =  MyWsHandler.getMap();
        Map<String, List<String>> clientArgument = MyWsHandler.getArgs();
        // 获取用户参数
        for (String key : sessionBeanMap.keySet()) {
            String jsonStr = JSONUtil.toJsonStr(new RealDTO("passing", countryService.getPassingRate(clientArgument.get(key)).getData()));
            sessionBeanMap.get(key).getWebSocketSession().sendMessage(new TextMessage(jsonStr));
        }
    }

    /**
     * 污水 水质分析
     */
    @Async
    @Scheduled(fixedDelay = 3600000) //1000=1秒
    public void foulContent() throws IOException {
        Map<String, SessionBean> sessionBeanMap =  MyWsHandler.getMap();
        Map<String, List<String>> clientArgument = MyWsHandler.getArgs();
        for (String key : sessionBeanMap.keySet()) {
            String jsonStr = JSONUtil.toJsonStr(new RealDTO("foulContent", countryService.getFoulContent(clientArgument.get(key)).getData()));
            sessionBeanMap.get(key).getWebSocketSession().sendMessage(new TextMessage(jsonStr));
        }
    }

    /**
     * 净水 水质分析
     */
    @Async
    @Scheduled(fixedDelay = 3600000) //1000=1秒
    public void pureContent() throws IOException {
        Map<String, SessionBean> sessionBeanMap =  MyWsHandler.getMap();
        Map<String, List<String>> clientArgument = MyWsHandler.getArgs();
        for (String key : sessionBeanMap.keySet()) {
            String jsonStr = JSONUtil.toJsonStr(new RealDTO("pureContent", countryService.getPureContent(clientArgument.get(key)).getData()));
            sessionBeanMap.get(key).getWebSocketSession().sendMessage(new TextMessage(jsonStr));
        }
    }

    /**
     * 循环水 水质分析
     */
    @Async
    @Scheduled(fixedDelay = 3600000) //1000=1秒
    public void circulatingContent() throws IOException {
        Map<String, SessionBean> sessionBeanMap = MyWsHandler.getMap();
        Map<String, List<String>> clientArgument = MyWsHandler.getArgs();
        for (String key : sessionBeanMap.keySet()) {
            String jsonStr = JSONUtil.toJsonStr(new RealDTO("circulatingContent", countryService.getCirculatingContent(clientArgument.get(key)).getData()));
            sessionBeanMap.get(key).getWebSocketSession().sendMessage(new TextMessage(jsonStr));
        }
    }

    /**
     * 污水 24h内 每2h水量
     */
    @Async
    @Scheduled(fixedDelay = 3600000) //1000=1秒
    public void foulAmount() throws IOException {
        Map<String, SessionBean> sessionBeanMap = MyWsHandler.getMap();
        Map<String, List<String>> clientArgument = MyWsHandler.getArgs();
        for (String key : sessionBeanMap.keySet()) {
            String jsonStr = JSONUtil.toJsonStr(new RealDTO("foulAmount", countryService.getFoulAmount(clientArgument.get(key)).getData()));
            sessionBeanMap.get(key).getWebSocketSession().sendMessage(new TextMessage(jsonStr));
        }
    }

    /**
     * 净水 24h内 每2h水量
     */
    @Async
    @Scheduled(fixedDelay = 3600000) //1000=1秒
    public void pureAmount() throws IOException {
        Map<String, SessionBean> sessionBeanMap = MyWsHandler.getMap();
        Map<String, List<String>> clientArgument = MyWsHandler.getArgs();
        for (String key : sessionBeanMap.keySet()) {
            String jsonStr = JSONUtil.toJsonStr(new RealDTO("pureAmount", countryService.getPureAmount(clientArgument.get(key)).getData()));
            sessionBeanMap.get(key).getWebSocketSession().sendMessage(new TextMessage(jsonStr));
        }
    }

    /**
     * 循环水 24h内 每2h水量
     */
    @Async
    @Scheduled(fixedDelay = 3600000) //1000=1秒
    public void circulatingAmount() throws IOException {
        Map<String, SessionBean> sessionBeanMap = MyWsHandler.getMap();
        Map<String, List<String>> clientArgument = MyWsHandler.getArgs();
        for (String key : sessionBeanMap.keySet()) {
            String jsonStr = JSONUtil.toJsonStr(new RealDTO("circulatingAmount", countryService.getCirculatingAmount(clientArgument.get(key)).getData()));
            sessionBeanMap.get(key).getWebSocketSession().sendMessage(new TextMessage(jsonStr));
        }
    }
}